package com.learn.core.service.impl;

import com.learn.core.domain.LCourseSort;
import com.learn.core.mapper.LCourseSortMapper;
import com.learn.core.service.ILCourseSortService;
import com.learn.framework.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther CodeGenerator
 * @create 2021-03-13 19:40:17
 * @describe 课程分类表服务实现类
 */
@Service
public class LCourseSortServiceImpl extends BaseServiceImpl<LCourseSortMapper, LCourseSort> implements ILCourseSortService {

    @Autowired
    LCourseSortMapper lCourseSortMapper;

    @Override
    public List<LCourseSort> queryLCourseSortAll() {
        List<LCourseSort> treeMenus =new ArrayList<>();
        List<LCourseSort> lCourseSortList = lCourseSortMapper.selectList(null);
        // 查询树结构
        for(LCourseSort lCourseSort : getDictTypeNode(lCourseSortList)) {
            lCourseSort=buildChilTree(lCourseSort,lCourseSortList);
            treeMenus.add(lCourseSort);
        }
        return treeMenus;
    }

    //递归，建立子树形结构
    private LCourseSort buildChilTree(LCourseSort pNode,List<LCourseSort> lCourseSortList){
        List<LCourseSort> chilCoursewareType =new ArrayList<>();
        for(LCourseSort lCourseSort : lCourseSortList) {
            if(lCourseSort.getParentNode() == pNode.getCourseSortId()) {
                chilCoursewareType.add(buildChilTree(lCourseSort,lCourseSortList));
            }
        }
        pNode.setChilLCourseSort(chilCoursewareType);
        return pNode;
    }

    //获取根节点
    private List<LCourseSort> getDictTypeNode(List<LCourseSort> lCourseSorts) {
        List<LCourseSort> lCourseSortList =new  ArrayList<LCourseSort>();
        for(LCourseSort lCourseSort : lCourseSorts) {
            if(lCourseSort.getParentNode() == 0) {
                lCourseSortList.add(lCourseSort);
            }
        }
        return lCourseSortList;
    }
}
